package dilmaj.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

import dilmaj.shared.AreaVO;
import dilmaj.shared.StarAreaVO;
import dilmaj.shared.StarCommentVO;
import dilmaj.shared.StarEntryVO;
import dilmaj.shared.StarLanguageVO;
import dilmaj.shared.StarSourceVO;
import dilmaj.shared.StarSuggestionVO;
import dilmaj.shared.StarTermVO;
import dilmaj.shared.StarUseCaseVO;

public class SearchStarHandler implements ClickHandler {
	private DilmajConstants constants = GWT.create(DilmajConstants.class);

	private StarTablePanel owner;
	//private StarEntryVO starVO;
	private TextBox dataWidget;
	private ListBox filterWidget;
	//private String filter;
	//private StarServiceAsync starSvc = GWT.create(StarService.class);
	
	public SearchStarHandler(TextBox widget, ListBox filterBox, StarTablePanel owner) {
		this.owner=owner;
		this.dataWidget=widget;
		this.filterWidget=filterBox;
	}

	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		//List<StarEntryVO> allEntries=
		DataStore.showWait();
		
		String filter="";
		
		for (int i=0;i<filterWidget.getItemCount();i++) {
			if (filterWidget.isItemSelected(i))
				filter=filter.concat(filterWidget.getValue(i));
		}

		String query=dataWidget.getText().toLowerCase();

		Object[] termsSorted=DataStore.getSortedTerms();
		Object[] areasSorted=DataStore.getSortedAreas();
		Object[] suggestionsSorted=DataStore.getSortedSuggestions();
		Object[] sourcesSorted=DataStore.getSortedSources();
		Object[] languagesSorted=DataStore.getSortedLanguages();
		Object[] usecasesSorted=DataStore.getSortedUseCases();
		Object[] commentsSorted=DataStore.getSortedComments();
		
		List<StarEntryVO> termsFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> sourcesFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> languagesFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> areasFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> suggestionsFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> commentsFound=new ArrayList<StarEntryVO>();
		List<StarEntryVO> usecasesFound=new ArrayList<StarEntryVO>();
		
		if (filter.contains(constants.term()) || filter.contains(constants.all())) {
			for (int i=0;i<termsSorted.length;i++) {
				StarTermVO termVO=(StarTermVO)termsSorted[i];
				if (termVO.getCaption().toLowerCase().contains(query))
					termsFound.add(termVO);
			}
		}

		if (filter.contains(constants.source()) || filter.contains(constants.all())) {
			for (int i=0;i<sourcesSorted.length;i++) {
				StarSourceVO sourceVO=(StarSourceVO)sourcesSorted[i];
				if (sourceVO.getCaption().toLowerCase().contains(query))
					sourcesFound.add(sourceVO);
			}
		}

		if (filter.contains(constants.language()) || filter.contains(constants.all())) {
			for (int i=0;i<languagesSorted.length;i++) {
				StarLanguageVO languageVO=(StarLanguageVO)languagesSorted[i];
				if (languageVO.getCaption().toLowerCase().contains(query))
					languagesFound.add(languageVO);
			}
		}

		if (filter.contains(constants.suggestion()) || filter.contains(constants.all())) {
			for (int i=0;i<suggestionsSorted.length;i++) {
				StarSuggestionVO suggestionVO=(StarSuggestionVO)suggestionsSorted[i];
				if (suggestionVO.getCaption().toLowerCase().contains(query))
					suggestionsFound.add(suggestionVO);
			}
		}
		
		if (filter.contains(constants.area()) || filter.contains(constants.all())) {
			for (int i=0;i<areasSorted.length;i++) {
				StarAreaVO areaVO=(StarAreaVO)areasSorted[i];
				if (areaVO.getCaption().toLowerCase().contains(query))
					areasFound.add(areaVO);
			}
		}
		
		if (filter.contains(constants.comment()) || filter.contains(constants.all())) {
			for (int i=0;i<commentsSorted.length;i++) {
				StarCommentVO commentVO=(StarCommentVO)commentsSorted[i];
				if (commentVO.getCaption().toLowerCase().contains(query))
					commentsFound.add(commentVO);
			}
		}
		
		if (filter.contains(constants.usecase()) || filter.contains(constants.all())) {
			for (int i=0;i<usecasesSorted.length;i++) {
				StarUseCaseVO usecaseVO=(StarUseCaseVO)usecasesSorted[i];
				if (usecaseVO.getCaption().toLowerCase().contains(query))
					usecasesFound.add(usecaseVO);
			}
		}

		DataStore.setFoundTerms(termsFound.toArray());
		DataStore.setFoundSources(sourcesFound.toArray());
		DataStore.setFoundLanguages(languagesFound.toArray());
		DataStore.setFoundAreas(areasFound.toArray());
		DataStore.setFoundSuggestions(suggestionsFound.toArray());
		DataStore.setFoundUseCases(usecasesFound.toArray());
		DataStore.setFoundComments(commentsFound.toArray());
		
		owner.populateTable(filter);
		
		DataStore.hideWait();
	}
}
